package com.biz.storage.config;

import com.biz.storage.annotation.*;
import com.biz.storage.enums.*;
import com.biz.storage.adapter.*;
import lombok.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.*;
import org.springframework.context.annotation.*;

import javax.annotation.*;
import java.util.*;

/**
 * @Description
 *      StorageService 处理类
 * @Author liuxi
 * @Date 2024/4/26
 **/
@Getter
@Setter
@Configuration
public class StorageConfig {

    @Value("${storage.service.type}")
    private EStorageType storageType;

    @Resource
    private ApplicationContext applicationContext;

    @Bean
    public StorageAdapter storageService() {
        Map<String, StorageAdapter> storageAdapterMap = this.applicationContext.getBeansOfType(StorageAdapter.class);
        for (StorageAdapter storageAdapter : storageAdapterMap.values()) {
            StorageAdapterType annotation = storageAdapter.getClass().getAnnotation(StorageAdapterType.class);
            if (storageType == annotation.value()) {
                return storageAdapter;
            }
        }
        throw new IllegalArgumentException("未找到对应的文件资源处理器");
    }

}
